package com.maoye.ossp.sys.base.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.ContractAtt;
import com.maoye.ossp.sys.base.entity.ContractAttType;
import com.maoye.ossp.sys.base.service.ContractAttService;
import com.maoye.ossp.sys.base.service.ContractAttTypeService;
import com.maoye.ossp.sys.base.utils.ImportAttType;

/**
 * 合同附件
 * 
 * @author linzhizhong
 * 
 */
@Controller
@RequestMapping(value = "sys/base/contractAtt/")
public class ContractAttController extends BaseController {

	@Resource
	ContractAttTypeService attTypeService;
	@Resource
	ContractAttService attServiece;

	@RequestMapping(value = "list")
	public String list(ContractAttType contractAttType,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		List<ContractAtt> list = attServiece.getAll();
		model.addAttribute("list", list);
		return "/sys/base/contractAtt/list";
	}

	@RequestMapping(value = "{id}/listAtt")
	public String listAtt(@PathVariable String id, String type,
			ContractAttType contractAttType, HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		List<ContractAtt> list = null;
		if (!"0".equals(type)) {
			list = attServiece.getContractAttByType(id);
		} else {
			list = attServiece.getAll();
		}
		model.addAttribute("typeId", id);
		model.addAttribute("list", list);
		return "/sys/base/contractAtt/attList";
	}

	@RequestMapping(value = "add")
	public String add(ContractAttType contractAttType,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("level", "0");
		List<ContractAttType> list = attTypeService.getContractAttType(map);
		ContractAttType parentAttype = null;
		if (list != null && list.size() != 0) {
			parentAttype = list.get(0);
		}
		model.addAttribute("parentAttype", parentAttype);
		return "/sys/base/contractAtt/addAttType";
	}

	@RequestMapping(value = "{id}/deleteAtt")
	public String deleteAtt(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		attServiece.remove(id);
		
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/base/contractAtt/00/listAtt";
	}

	@RequestMapping(value = "{id}/deleteAttType")
	public String deleteAttType(@PathVariable Long id, Model model,
			RedirectAttributes redirectAttributes) {
		attTypeService.remove(id);
		attTypeService.removeByParentId(id);
		attServiece.removeByattType(id.toString());
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/sys/base/contractAtt/00/listAtt";
	}

	@RequestMapping(value = "{typeid}/addAtt")
	public String addAtt(@PathVariable String typeid, ContractAtt contractAtt,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		contractAtt.setAttId(Long.parseLong(typeid));
		return "/sys/base/contractAtt/addAtt";
	}

	@RequestMapping(value = "{id}/{typeId}/editAtt")
	public String editAtt(@PathVariable String id, @PathVariable String typeId,
			ContractAtt contractAtt, HttpServletRequest request,
			HttpServletResponse response, Model model) {

		contractAtt = attServiece.get(Long.parseLong(id));
		model.addAttribute("attId", typeId);
		model.addAttribute("contractAtt", contractAtt);
		return "/sys/base/contractAtt/addAtt";
	}

	@RequestMapping(value = "save")
	public String save(ContractAttType contractAttType,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {

		contractAttType.setId(idGenerator.getUId());
		attTypeService.create(contractAttType);

		return "redirect:/sys/base/contractAtt/list";

	}

	@RequestMapping(value = "saveAtt")
	public String saveAtt(ContractAtt contractAtt, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		if (contractAtt.getId() != null) {
			attServiece.update(contractAtt);
		} else {
			contractAtt.setId(idGenerator.getUId());
			attServiece.create(contractAtt);
		}
		return "redirect:/sys/base/contractAtt/" + contractAtt.getAttId()
				+ "/listAtt";
	}

	@RequestMapping(value = "get")
	public void get(ContractAttType contractAttType,
			HttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		StringBuffer json = new StringBuffer();
		// map.put("id",_menu.getId());
		ArrayList<ContractAttType> list = (ArrayList<ContractAttType>) attTypeService
				.getAll();
		json.append("[");
		for (int i = 0; i < list.size(); i++) {
			json.append("{");
			json.append("name:\"").append(list.get(i).getName()).append("\""); // name
			json.append(",");
			json.append("id:").append("\"").append(list.get(i).getId())
					.append("\""); // id
			json.append(",");
			json.append("pId:")
					.append("\"")
					.append(list.get(i).getParentId() != null ? list.get(i)
							.getParentId() : 0).append("\""); // pid
			json.append(",");
			json.append("open:").append("true");
			json.append(",");
			json.append("isParent:").append(
					list.get(i).getIsLeaf() == 0 ? false : true); // isParent;
			json.append("}");
			json.append(",");
		}
		String outJson = json.substring(0, json.length() - 1).toString();
		outJson = outJson + ("]");
		out.write(outJson);
		out.flush();
		out.close();
	}
	
	@RequestMapping(value = "remove")
	public void remove(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
		
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		StringBuffer json = new StringBuffer();
		
	}
	

	@RequestMapping("{typeid}/importAttType")
	public String importAttType(MultipartHttpServletRequest request, HttpServletResponse response, ContractAttType contractAttType, @PathVariable Long typeid, Model model, RedirectAttributes redirectAttributes) {
		MultipartFile fileLoad = request.getFile("excelFile");
		ArrayList<ArrayList<String>> list = ImportAttType.readExcel(fileLoad);	//所得所有excel数据
		for (int i = 0; i < list.size(); i++) {
			ArrayList<String> strList = list.get(i);	//获得一列
			String s = strList.get(0).replace(".0", "");
			
			if(!isNumeric(s)){
				contractAttType.setId(idGenerator.getUId());
				contractAttType.setName(strList.get(1));
				contractAttType.setParentId(typeid.toString());
				attTypeService.create(contractAttType);
				continue;
			}
			ContractAtt att = new ContractAtt();
			att.setId(idGenerator.getUId());
			att.setName(strList.get(1));
			att.setAttId(contractAttType.getId());
			attServiece.create(att);
		}
		addMessage(redirectAttributes, "保存成功！！！");
		return "/sys/base/contractAtt/import";
	}
	
	@RequestMapping("{typeid}/showImport")
	public String showImport(ContractAttType contractAttType, @PathVariable Long typeid, String excelFile, Model model){
		model.addAttribute("typeid", typeid);
		model.addAttribute("attType", contractAttType);
		return "/sys/base/contractAtt/import";
	}
	
	/**
	 * 判断是否数字
	 */
	public static boolean isNumeric(String str) {
		try{
			Integer.parseInt(str);
			return true;
		}catch(Exception e){
			return false;
		}
	}

}
